#!/bin/bash
VERSIONS=$(cvs -q status -l | grep "Working revision" | sed 's/.*Working revision:[[:space:]]*//' | sed 's/[[:digit:]]*\.\(.*\)/\1/')
SUM=0

for num in $VERSIONS; do
  SUM=$((${SUM}+${num}))
done


VERSIONSREC=$(cvs -q status | grep "Working revision" | sed 's/.*Working revision:[[:space:]]*//' | sed 's/[[:digit:]]*\.\(.*\)/\1/')
SUMREC=0

for num in $VERSIONSREC; do
  SUMREC=$((${SUMREC}+${num}))
done

cat << EOF

Sum_Versions v1.0 - (c) Antti Maula

Also remember that if parameter is given, it is used as a project name for
generating per-project header file with the same information.

Sum of all local versions is ${SUM}
Sum of all recursive versions is ${SUMREC}
And GrepAbles:

VERSIONSUM: ${SUM}
VERSIONSUMREC: ${SUMREC} 

EOF

# Check for parameter.
if [ $# -eq 1 ]; then
echo "Project name given, generating headers..."
HDRFILE="${1}_versionsum.hpp"
echo -n "Creating header file '${HDRFILE}'..."
UPPERNAME=$(echo ${1} | tr "[:lower:]" "[:upper:]")

cat << EOF2 > ${HDRFILE}
/**
 * \file
 * \brief Autogenerated Sum-Version header for '${1}'
 * \author sum_versions - generator script
 */
#ifndef _HEADER_${UPPERNAME}_VERSIONSUM_HPP_
#define _HEADER_${UPPERNAME}_VERSIONSUM_HPP_

#define ${UPPERNAME}_VERSION_SUM             ${SUM}
#define ${UPPERNAME}_VERSION_RECURSIVE_SUM   ${SUMREC}

#endif // _HEADER_${UPPERNAME}_VERSIONSUM_HPP_

EOF2

echo -e "Done.\n"
fi
